/*
		ServEngine framework library
		A library to fast develop JEE online applications
	
		Copyright 2008 José Ignacio de Córdoba Álvaro

		Licensed under the Apache License, Version 2.0 (the "License");
		you may not use this file except in compliance with the License.
		You may obtain a copy of the License at

				http://www.apache.org/licenses/LICENSE-2.0

		Unless required by applicable law or agreed to in writing, software
		distributed under the License is distributed on an "AS IS" BASIS,
		WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
		See the License for the specific language governing permissions and
		limitations under the License.
	
		Jose Ignacio de Cordoba Alvaro
		http://ignacio.decordoba.com
*/

package com.servengine.user;

import com.servengine.portal.Portal;

import java.util.ArrayList;
import java.util.Collection;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.CascadeType;
import javax.persistence.UniqueConstraint;

@Entity
@Table (uniqueConstraints = {@UniqueConstraint(columnNames={"name","portal_id"})})
public class ExtraUserdataField implements java.io.Serializable,Comparable<ExtraUserdataField>
{
	Portal portal;

	Integer id;

	String name, defaultValue;

	boolean required, publicField;

	String options;

	Set<ExtraUserdata> data;

	public ExtraUserdataField() {}

	public ExtraUserdataField(Portal portal, String name)
	{
		data = new HashSet<ExtraUserdata>();
		this.portal = portal;
		this.name=name;
	}

	public int compareTo(ExtraUserdataField o)
	{
		return name.compareTo(o.getName());
	}

	public boolean equals (Object item)
	{
		return ((ExtraUserdataField)item).getId()==getId();
	}

	@Id @GeneratedValue
	public java.lang.Integer getId()
	{
		return id;
	}

	public void setId(Integer id)
	{
		this.id = id;
	}
	
	public String getName()
	{
		return name;
	}

	public void setOptions(String value)
	{
		options = value;
	}

	@Lob
	public String getOptions()
	{
		return options;
	}

	/**
	 * @deprecated
	 */
	@Transient
	public Collection<ExtraUserdata> getOptionsList()
	{
		return getOptionList();
	}
	@Transient
	public Collection<ExtraUserdata> getOptionList()
	{
		Collection<ExtraUserdata> list = new ArrayList<ExtraUserdata>();
		if (options != null)
			for (String option: options.split(","))
				list.add(new ExtraUserdata(this, null, option));
		return list;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void setRequired(boolean required)
	{
		this.required = required;
	}

	public boolean isRequired()
	{
		return required;
	}

	public void setPublicfield(boolean publicField)
	{
		this.publicField = publicField;
	}

	public boolean isPublicField()
	{
		return publicField;
	}

	public void setDefaultValue(String defaultValue)
	{
		this.defaultValue = defaultValue;
	}

	public String getDefaultValue()
	{
		return defaultValue;
	}

	public void setPortal(Portal portal)
	{
		this.portal = portal;
	}

	@ManyToOne(fetch=FetchType.EAGER, optional=false)
	public Portal getPortal()
	{
		return portal;
	}

	public void setPublicField(boolean publicField)
	{
		this.publicField = publicField;
	}

	public void setData(Set<ExtraUserdata> data)
	{
		this.data = data;
	}

	@OneToMany(mappedBy="field",fetch=FetchType.LAZY, cascade={CascadeType.REMOVE})
//	@Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
	public Set<ExtraUserdata> getData()
	{
		return data;
	}
}
